//
//  ADDmenuVC.m
//  reSearchDemo
//
//  Created by 科技 on 15/8/28.
//  Copyright (c) 2015年 Kiwaro. All rights reserved.
//

#import "ADDmenuVC.h"

@interface ADDmenuVC ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate> {
    UITableView *_tableView;
    NSMutableArray *_dataSource;
    UITextField *nameText;
    UITextField *pathText;
}

@end

@implementation ADDmenuVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"添加菜单";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存菜单" style:UIBarButtonItemStyleDone target:self action:@selector(saveMenuBtnAction:)];
    
    [self loadDataSource];
    [self allocTableView];
}
//初始化tableview
- (void)allocTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ViewWIDTH(self.view), ViewHEIGHT(self.view)) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    
    [self.view addSubview:_tableView];
}
//加载数据
- (void)loadDataSource {
    _dataSource = [NSMutableArray array];
    [_dataSource addObject:@"菜单类型:"];
    [_dataSource addObject:@"菜单名字:"];
    [_dataSource addObject:@"链接:"];
}
#pragma mark --保存菜单按钮点击事件--
- (void)saveMenuBtnAction:(UIBarButtonItem *)sender {
    [self showText:@"没错,本大爷就是保存按钮!"];
    if (!nameText.text.length || !pathText.text.length) {
        UIAlertView *alert = [self showAlertViewTitle:@"提示:" message:@"内容不能为空" Btntitle:@"确定"];
        [alert show];
        return;
    }
    NSString *path;
    NSDictionary *param;
    if (![self.menuDic[@"type"] isEqualToString:@"menu"]) {
        path = @"MobilePort/Index/add_menu_type_1";
        param = @{@"menuid":self.menuDic[@"menuid"],@"name":nameText.text,@"link":pathText.text};
    }else {
        path = @"MobilePort/Index/add_menu_type_0";
        param = @{@"name":nameText.text,@"link":pathText.text};
    }
    
    [[HTTPGlobals shareHTTPGlobal] PostHTTPRequest:path parameters:param finish:^(BOOL seccess, NSString *error, id obj) {
        if (seccess) {
            DLog(@"添加返回数据  %@",obj);
            if (![obj[@"code"] intValue]) {
                XLblock block = self.loadBlcok;
                if (block) {
                    block(YES);
                }
                [self showText:@"添加成功"];
            }else {
                UIAlertView *alert = [self showDefaultAlertView];
                [alert show];
            }
            
        }else {
            DLog(@"error = %@",error);
        }
    }];
    
}

#pragma mark -
#pragma mark --tableviewDelegate--
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
    }
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 80, 44)];
    textLabel.text = _dataSource[indexPath.row];
    textLabel.textAlignment = NSTextAlignmentRight;
    [cell addSubview:textLabel];
    switch (indexPath.row) {
        case 0:
        {
            UILabel *menu = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(textLabel.frame) + 5, CGRectGetMinY(textLabel.frame), 100, CGRectGetHeight(textLabel.frame))];
            menu.text = self.menuDic[@"name"];
            menu.textColor = [UIColor grayColor];
            [cell addSubview:menu];
            
            
           break;
        }
        case 1:
        {
            nameText = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(textLabel.frame) + 5, CGRectGetMinY(textLabel.frame), 200, CGRectGetHeight(textLabel.frame))];
            [UIView SetRoundedCorners:nameText andcornerRadius:5.0 andborderWidth:1.0 andborderColor:[UIColor lightGrayColor]];
            nameText.placeholder = @"不多于五个字";
            nameText.delegate = self;
            [cell addSubview:nameText];
            break;
        }
        case 2:
        {
            pathText = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(textLabel.frame) + 5, CGRectGetMinY(textLabel.frame), 200, CGRectGetHeight(textLabel.frame))];
            [UIView SetRoundedCorners:pathText andcornerRadius:5.0 andborderWidth:1.0 andborderColor:[UIColor lightGrayColor]];
            pathText.placeholder = @"例如:www.baidu.com";
            pathText.delegate = self;
            [cell addSubview:pathText];
            break;
        }
            
            
        default:
            break;
    }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 64;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark --ScrollViewDelegate --
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [nameText resignFirstResponder];
    [pathText resignFirstResponder];
}

#pragma mark -
#pragma mark --textFieldDelegate--
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
